﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Besture.Bll;
using Besture.Common;
using Besture.Model;
using Besture.Utils;
using Besture.Common.Definition;

namespace Besture.UI.Admin.Service
{
    public partial class ProductSet : BasePage
    {
        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            if (!IsPostBack)
            {
                bindProductType();
                bindProductState();

                if (Request.QueryString["id"] != null && !string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    //编辑编号
                    hidEditID.Value = Request.QueryString["id"];
                    if (!Fun.IsNumber(hidEditID.Value))
                    {
                        RedirectErrorUrl("编号错误");
                    }
                    InitData(Convert.ToInt32(hidEditID.Value));
                }
            }
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            ProductDac dac = new ProductDac();
            ProductModel model = dac.GetModel(id);
            txtProductName.Text = model.ProductName != null ? model.ProductName.ToString() : "";
            txtProductCode.Text = model.ProductCode != null ? model.ProductCode.ToString() : "";
            txtPreYield.Text = model.PreYield != null ? model.PreYield.ToString() : "";
            txtLowerFund.Text = model.LowerFund != null ? model.LowerFund.ToString() : "";
            txtDeadline.Text = model.Deadline != null ? model.Deadline.ToString() : "";
            txtSigning.Text = model.Signing != null ? model.Signing.ToString() : "";
            txtOpeningFrequency.Text = model.OpeningFrequency != null ? model.OpeningFrequency.ToString() : "";
            txtPreYearYield.Text = model.PreYearYield != null ? model.PreYearYield.ToString() : "";
            txtProductDeadline.Text = model.ProductDeadline != null ? model.ProductDeadline.ToString() : "";
            txtPubDate.Text = model.PubDate != null ? model.PubDate.ToString() : "";
            txtRaiseScale.Text = model.RaiseScale != null ? model.RaiseScale.ToString() : "";
            txtInvestStart.Text = model.InvestStart != null ? model.InvestStart.ToString() : "";
            txtYieldAllot.Text = model.YieldAllot != null ? model.YieldAllot.ToString() : "";
            ddlProductType.SelectedValue = model.ProductType.ToString();
            ddlProductState.SelectedValue = model.ProductState.ToString();
            txtRemark.Text = model.Remark != null ? model.Remark.ToString() : "";
            txtSortID.Text = model.SortID != null ? model.SortID.ToString() : "";

        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            ProductDac dac = new ProductDac();
            ProductModel model = null;
            string editID = hidEditID.Value;
            //添加
            if (string.IsNullOrEmpty(editID))
            {
                model = new ProductModel();
                model.ProductName = txtProductName.Text.Trim();
                model.ProductCode = txtProductCode.Text.Trim();
                model.PreYield = txtPreYield.Text.Trim();
                model.LowerFund = txtLowerFund.Text.Trim();
                model.Deadline = txtDeadline.Text.Trim();
                model.Signing = txtSigning.Text.Trim();
                model.OpeningFrequency = txtOpeningFrequency.Text.Trim();
                model.PreYearYield = txtPreYearYield.Text.Trim();
                model.ProductDeadline = txtProductDeadline.Text.Trim();
                model.PubDate = txtPubDate.Text.Trim() != "" ? Convert.ToDateTime(txtPubDate.Text.Trim()) : DateTime.Now;
                model.RaiseScale = txtRaiseScale.Text.Trim();
                model.InvestStart = txtInvestStart.Text.Trim();
                model.YieldAllot = txtYieldAllot.Text.Trim();
                model.ProductType = ddlProductType.SelectedValue != "" ? Convert.ToInt32(ddlProductType.SelectedValue) : 0;
                model.ProductState = ddlProductState.SelectedValue != "" ? Convert.ToInt32(ddlProductState.SelectedValue) : 0;
                model.Remark = txtRemark.Text.Trim();
                model.SortID = txtSortID.Text.Trim() != "" ? Convert.ToInt32(txtSortID.Text.Trim()) : 0;
                model.CreateBy = LoginUserInfo.ID;
                model.CreateTime = DateTime.Now;
                model.UpdateBy = LoginUserInfo.ID;
                model.UpdateTime = DateTime.Now;
                model.IsDeleted = 0;

                dac.Add(model);
            }
            //编辑
            else
            {
                model = dac.GetModel(Convert.ToInt32(editID));
                model.ProductName = txtProductName.Text.Trim();
                model.ProductCode = txtProductCode.Text.Trim();
                model.PreYield = txtPreYield.Text.Trim();
                model.LowerFund = txtLowerFund.Text.Trim();
                model.Deadline = txtDeadline.Text.Trim();
                model.Signing = txtSigning.Text.Trim();
                model.OpeningFrequency = txtOpeningFrequency.Text.Trim();
                model.PreYearYield = txtPreYearYield.Text.Trim();
                model.ProductDeadline = txtProductDeadline.Text.Trim();
                model.PubDate = txtPubDate.Text.Trim() != "" ? Convert.ToDateTime(txtPubDate.Text.Trim()) : DateTime.Now;
                model.RaiseScale = txtRaiseScale.Text.Trim();
                model.InvestStart = txtInvestStart.Text.Trim();
                model.YieldAllot = txtYieldAllot.Text.Trim();
                model.ProductType = ddlProductType.SelectedValue != "" ? Convert.ToInt32(ddlProductType.SelectedValue) : 0;
                model.ProductState = ddlProductState.SelectedValue != "" ? Convert.ToInt32(ddlProductState.SelectedValue) : 0;
                model.Remark = txtRemark.Text.Trim();
                model.SortID = txtSortID.Text.Trim() != "" ? Convert.ToInt32(txtSortID.Text.Trim()) : 0;
                model.UpdateBy = LoginUserInfo.ID;
                model.UpdateTime = DateTime.Now;
                model.IsDeleted = 0;

                dac.Update(model);
            }
            Fun.Alert("保存成功", "ProductList.aspx");
        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {
            RedirectUrl("ProductList.aspx");
        }

        #endregion

        /// <summary>
        ///绑定产品类型
        /// </summary>
        public void bindProductType()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_Product);
            this.ddlProductType.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlProductType.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }
        /// <summary>
        ///绑定状态
        /// </summary>
        public void bindProductState()
        {
            DictionaryDac dac = new DictionaryDac();
            DataTable dt = dac.GetDataTableByType(Constants.DICTIONARY_TYPE_StateClass);
            this.ddlProductState.Items.Add(new ListItem("请选择", ""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddlProductState.Items.Add(new ListItem(dt.Rows[i]["Name"].ToString(), dt.Rows[i]["Code"].ToString()));
            }
        }

    }
}
